package com.difsonapps.android.marker.adapters;

import com.difsonapps.android.marker.FeedImageView;
import com.difsonapps.android.marker.R;
import com.difsonapps.android.marker.app.AppController;
import com.difsonapps.android.marker.beans.facebook.Album;
 
import java.util.List;
 
import android.app.Activity;
import android.content.Context;
import android.text.Html;
import android.text.TextUtils;
import android.text.format.DateUtils;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
 
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
 
public class ImportFacebookAlbumsAdapter extends BaseAdapter {  
    private Activity activity;
    private LayoutInflater inflater;
    private List<Album> albums;
    ImageLoader imageLoader = AppController.getInstance().getImageLoader();
 
    public ImportFacebookAlbumsAdapter(Activity activity, List<Album> albums) {
        this.activity = activity;
        this.albums = albums;
    }
 
    @Override
    public int getCount() {
        return albums.size();
    }
 
    @Override
    public Object getItem(int location) {
        return albums.get(location);
    }
 
    @Override
    public long getItemId(int position) {
        return position;
    }
 
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
 
        if (inflater == null) inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) convertView = inflater.inflate(R.layout.album_facebook_import_item, null);
 
        if (imageLoader == null) imageLoader = AppController.getInstance().getImageLoader();
        
        CheckBox is_checked = (CheckBox)convertView.findViewById(R.id.is_checked);
        NetworkImageView cover_image = (NetworkImageView) convertView.findViewById(R.id.cover_image);
        TextView album_name = (TextView) convertView.findViewById(R.id.album_name);
        TextView count_photos = (TextView) convertView.findViewById(R.id.count_photos);
        ProgressBar statusProgressBar = (ProgressBar) convertView.findViewById(R.id.statusProgressBar);
        ImageView status_icon = (ImageView) convertView.findViewById(R.id.status_icon);
        TextView status_message = (TextView) convertView.findViewById(R.id.status_message);
        
        View status_layout = convertView.findViewById(R.id.status_layout);
        
        Album item = albums.get(position);
        if(item!=null){
        	is_checked.setChecked(item.isCheched());
        	is_checked.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					checkAlbum(position, ((CheckBox)v).isChecked());
					
				}
			});
        	//TODO Set event to check
        	if(item.getCoverPhotoSrc()!=null) cover_image.setImageUrl(item.getCoverPhotoSrc(), imageLoader);
        	album_name.setText(item.getName());
        	count_photos.setText(""+item.getCount());
        	status_layout.setVisibility(item.getStatus()!=Album.STATUS_CREATED && item.getStatus()!=Album.STATUS_STARTED?View.VISIBLE:View.GONE);
        	statusProgressBar.setVisibility(item.getStatus()==Album.STATUS_PENDING?View.VISIBLE:View.GONE);
        	status_icon.setVisibility(item.getStatus()==Album.STATUS_IMPORTED||item.getStatus()==Album.STATUS_FAILED?View.VISIBLE:View.GONE);
        	if(item.getStatus()==Album.STATUS_IMPORTED) {
        		status_icon.setImageResource(R.drawable.ic_ok);
        		status_message.setText(""+item.getCountUploaded()+" photo(s) imported");
        	}
        	else if(item.getStatus()==Album.STATUS_FAILED) {
        		status_icon.setImageResource(R.drawable.ic_error);
        		status_message.setText("Failed");
        	}
        	else if(item.getStatus()==Album.STATUS_PENDING) {
        		status_message.setText("Pending");
        	}
        	else {
        		status_message.setText("");
        	} 
        }
        return convertView;
    }
    
    public void checkAlbum(int positon, boolean value){
    	Album album = albums.get(positon);
    	if(album!=null) {
    		album.setCheched(value);
    		toast("Checked : "+album.isCheched());
    	}
    }
    public void toast(String text){
		Toast.makeText(this.activity, text, Toast.LENGTH_SHORT).show();
	}
}
